package com.agilegroup.crm.ui;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockListActivity;
import com.actionbarsherlock.view.MenuItem;
import com.agilegroup.crm.AgileApplication;
import com.agilegroup.crm.adapters.ViewDetailsAdapter;
import com.agilegroup.crm.model.Appointment;
import com.agilegroup.crm.model.EntryItem;
import com.agilegroup.crm.model.Item;
import com.agilegroup.crm.model.SectionItem;
import com.agilegroup.crm.requests.ViewAppointmentJsonRequest;
import com.agilegroup.crm.utils.SessionManager;
import com.octo.android.robospice.persistence.DurationInMillis;
import com.octo.android.robospice.persistence.exception.SpiceException;
import com.octo.android.robospice.request.listener.RequestListener;


/**
 * @author Hisham
 *
 */
@SuppressLint("SimpleDateFormat")
public class ViewAppointmentActivity extends SherlockListActivity {
    /** Called when the activity is first created. */
	
	private SessionManager sessionManager;
	private ArrayList<Item> items = new ArrayList<Item>();
	private static final String JSON_CACHE_KEY = "view_appointment_json";
	
	@SuppressWarnings("static-access")
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        Intent intent = getIntent();
        String appointmentId = intent.getStringExtra("appointmentId");
        Log.d("appointmentId", appointmentId);
        sessionManager = new SessionManager(getBaseContext());    
    	HashMap<String, String> user = sessionManager.getUserDetails();
    	String empId = user.get(sessionManager.KEY_ID);
        ArrayList<String>parameters = new ArrayList<String>();
        parameters.add(appointmentId);
        parameters.add(empId);
        AgileApplication.getInstance().spiceManager.execute(new ViewAppointmentJsonRequest(parameters),JSON_CACHE_KEY,DurationInMillis.ALWAYS_EXPIRED,new ViewAppointmentJsonRequestListener());
    }
    
    public class ViewAppointmentJsonRequestListener implements RequestListener<Appointment>{

		public void onRequestFailure(SpiceException spiceException) {
			Toast.makeText(ViewAppointmentActivity.this, "Failure", Toast.LENGTH_SHORT).show();
		}
		public void onRequestSuccess(Appointment appointment) {
			SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
			items.add(new SectionItem("Appointment Information"));
			if(appointment.getOwnerName()!=null&&!appointment.getOwnerName().equalsIgnoreCase("")){
				items.add(new EntryItem("Appointment Owner", appointment.getOwnerName()));
			}
			else{
				items.add(new EntryItem("Appointment Owner", "  "));
			}
			if(appointment.getSalesManName()!=null&&!appointment.getSalesManName().equalsIgnoreCase("")){
				items.add(new EntryItem("Sales Man", appointment.getSalesManName()));
			}
			else{
				items.add(new EntryItem("Sales Man", "  "));
			}
			if(appointment.getContactedPersonName()!=null&&!appointment.getContactedPersonName().equalsIgnoreCase("")){
				items.add(new EntryItem("Contacted Person", appointment.getContactedPersonName()));
			}
			else{
				items.add(new EntryItem("Contacted Person", "   "));
			}
			if(appointment.getComment()!=null&&!appointment.getComment().equalsIgnoreCase("")){
				 items.add(new EntryItem("Comment", appointment.getComment()));
			}
			else{
				items.add(new EntryItem("Comment", "   "));
			}
			if(appointment.getDay()!=null&&!appointment.getDay().equalsIgnoreCase("")){
				items.add(new EntryItem("Day", appointment.getDay()));
			}
			else{
				items.add(new EntryItem("Day", "   "));
			}
			
			if(appointment.getDate()!=null){
		        String formattedDate = formatter.format(appointment.getDate());
				items.add(new EntryItem("Date", formattedDate));
			}
			else{
				items.add(new EntryItem("Date", "   "));
			}
			if(appointment.getTime()!=null&&!appointment.getTime().equalsIgnoreCase("")){
				items.add(new EntryItem("Starting Time", appointment.getTime()));
			}
			else{
				items.add(new EntryItem("Starting Time", "   "));
			}
			if(appointment.getMeetingDuration()!=null&&!appointment.getMeetingDuration().equalsIgnoreCase("")){
				items.add(new EntryItem("Ending Time", appointment.getMeetingDuration()));
			}
			else{
				items.add(new EntryItem("Ending Time", "   "));
			}
	        
	        items.add(new SectionItem("Meeting Information"));
	        if(appointment.getConfirmationDateExpected()!=null&&!appointment.getConfirmationDateExpected().equalsIgnoreCase("")){
	        	if(appointment.getConfirmationDateExpected().equals("true")){
	        		items.add(new EntryItem("Confirmation date expected", "Yes"));
	        	}
	        	else{
	        		items.add(new EntryItem("Confirmation date expected", "No"));
	        	}
	        	
	        }
	        else{
				items.add(new EntryItem("Confirmation date expected", "   "));
			}
	        if(appointment.getMeetingDone()!=null){
	        	if(appointment.getMeetingDone()==true){
	        		items.add(new EntryItem("Meeting done", "Yes"));
	        	}
	        	else{
	        		items.add(new EntryItem("Meeting done", "No"));
	        	}
	        }
	        else{
				items.add(new EntryItem("Meeting done", "   "));
			}
	        if(appointment.getCustomerNeed()!=null&&!appointment.getCustomerNeed().equalsIgnoreCase("")){
	        	items.add(new EntryItem("Customer need", appointment.getCustomerNeed()));
	        }
	        else{
				items.add(new EntryItem("Customer need", "   "));
			}
	        if(appointment.getDemoDone()!=null){
	        	if(appointment.getDemoDone()==true){
	        		items.add(new EntryItem("Demo done", "Yes"));
	        	}
	        	else{
	        		items.add(new EntryItem("Demo done", "No"));
	        	}
	        }
	        else{
				items.add(new EntryItem("Demo done", "   "));
			}
	        if(appointment.getOfferSubmitted()!=null){
	        	if(appointment.getOfferSubmitted()==true){
	        		items.add(new EntryItem("Offer submitted", "Yes"));
	        	}
	        	else{
	        		items.add(new EntryItem("Offer submitted", "No"));
	        	}
	        }
	        else{
				items.add(new EntryItem("Offer submitted", "   "));
			}
	        if(appointment.getDiscountDone()!=null){
	        	if(appointment.getDiscountDone()==true){
	        		items.add(new EntryItem("Discount done", "Yes"));
	        	}
	        	else{
	        		items.add(new EntryItem("Discount done", "No"));
	        	}
	        }
	        else{
				items.add(new EntryItem("Discount done", "   "));
			}
	        
	        if(appointment.getCompatitorOffer()!=null){
	        	if(appointment.getCompatitorOffer()==true){
	        		items.add(new EntryItem("Competitor offer", "Yes"));
	        	}
	        	else{
	        		items.add(new EntryItem("Competitor offer", "No"));
	        	}
	        }
	        else{
				items.add(new EntryItem("Competitor offer", "   "));
			}
	        if(appointment.getReplyPeriod()!=null){
	        	items.add(new EntryItem("Reply period", appointment.getReplyPeriod()));
	        }
	        else{
				items.add(new EntryItem("Reply period", "   "));
			}
	        if(appointment.getDealClosingDone()!=null){
	        	if(appointment.getDealClosingDone()==true){
	        		items.add(new EntryItem("Deal closing done", "Yes"));
	        	}
	        	else if(appointment.getDealClosingDone()==false){
	        		items.add(new EntryItem("Deal closing done", "No"));
	        	}
	        }
	        else{
				items.add(new EntryItem("Deal closing done", "   "));
			}
	        if(appointment.getDealClosingReason()!=null){
	        	items.add(new EntryItem("Deal closing reason", appointment.getDealClosingReason()));
	        }
	        else{
				items.add(new EntryItem("Deal closing reason", "   "));
			}
	        
	        
	        items.add(new SectionItem("Next Follow Up Information"));
	        if(appointment.getNextFollowUp()!=null){
	        	if(appointment.getNextFollowUp()==true){
	        		items.add(new EntryItem("Next Follow Up", "Yes"));
	        	}
	        	else{
	        		items.add(new EntryItem("Next Follow Up", "No"));
	        	}
	        }
	        else{
				items.add(new EntryItem("Next Follow Up", "   "));
			}
	        if(appointment.getFollowUpType()!=null&&!appointment.getFollowUpType().equalsIgnoreCase("")){
	        	if(appointment.getFollowUpType().equals("appointment")){
	        		items.add(new EntryItem("Next Follow Up Type", "Appointment"));
	        	}
	        	else{
	        		if(appointment.getFollowUpType().equals("none")){
	        			items.add(new EntryItem("Next Follow Up Type", "   "));
	        		}
	        		else{
	        			items.add(new EntryItem("Next Follow Up Type", "Call"));
	        		}
	        	}
	        }
	        else{
				items.add(new EntryItem("Next Follow Up Type", "   "));
			}
	        if(appointment.getFollowUpDate()!=null){
	        	String formattedDate = formatter.format(appointment.getFollowUpDate());
	        	items.add(new EntryItem("Next Follow Up Date", formattedDate));
	        }
	        else{
				items.add(new EntryItem("Next Follow Up Date", "   "));
			}
	        if(appointment.getFollowUpDetails()!=null&&!appointment.getFollowUpDetails().equalsIgnoreCase("")){
	        	items.add(new EntryItem("Next Follow Up Details", appointment.getFollowUpDetails()));
	        }
	        else{
				items.add(new EntryItem("Next Follow Up Details", "   "));
			}
	        ViewDetailsAdapter adapter = new ViewDetailsAdapter(ViewAppointmentActivity.this, items);
	        setListAdapter(adapter);
		}
	 }
    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
    	int itemId = item.getItemId();
        switch (itemId) {
        case android.R.id.home:
             finish();
            break;

        }

        return true;
    }
}
